package sskj.regulatory.presentation.screen.authentication.projectdepartment

import com.alipay.sdk.app.statistic.c.v
import com.vondear.rxtools.RxDataTool
import isskj.regulatory.presentation.base_mvp.api.ApiPresenter
import sskj.regulatory.data.response.Response
import sskj.regulatory.data.response.eng.EngResponse
import sskj.regulatory.data.response.session.IndexDataResponse
import sskj.regulatory.domain.entity.user.BingDingEntity
import sskj.regulatory.domain.fetcher.result_listener.RequestType
import sskj.regulatory.presentation.constans.Constans
import sskj.regulatory.ribble.presentation.utils.extensions.isNull
import javax.inject.Inject

class ProjectDeptPresenter @Inject constructor()
    : ApiPresenter<ProjectDeptContract.View>(), ProjectDeptContract.Presenter {


    private var indexData: IndexDataResponse? = null

    override fun bingDing(username: String, password: String) {
        view.showLoading()
        fetch(userInteractor.bingDing(BingDingEntity(null, indexData?.data?.simpleUser?.id, Constans.XMB_ROLE_ID,
                Constans.XMB_ROLE_NAME, username, username, password)),
                RequestType.BINGDING) {
            when (it.success) {
                true -> {
                    view.hideLoading()
                    view.bingDingOk()
                }
                else -> view.showError(it.msg)
            }
        }
    }

    override fun delBingDing() {
        view.showLoading()
        fetch(userInteractor.delBingDing(indexData?.getBingDingByRole(Constans.XMB_ROLE_ID)?.id.toString())) {
            when (it.success) {
                true -> {
                    fetch(userInteractor.getIndexData()) {
                        view.hideLoading()
                        indexData = it
                        view.initOk(null)
                        view.delBingDingOk()
                    }
                }
                else -> view.showError(it.msg)
            }
        }
    }

    override fun indexData() {
        view.showLoading()
        fetch(userInteractor.getIndexData()) {
            indexData = it
            val bingDingResponse = it.getBingDingByRole(Constans.XMB_ROLE_ID)
            view.hideLoading()
            when (bingDingResponse?.isNull) {
                false -> {
                    view.showLoading()
                    fetch(userInteractor.xmbAuth(bingDingResponse.userName.toString(), bingDingResponse.pwd.toString()), RequestType.XMB_AUTH) {
                        view.hideLoading()
                        when (it.success) {
                            true -> {
                                view.showBingDing(RxDataTool.hideMobilePhone4(bingDingResponse.userName
                                        ?: "13000000000"), "你共参与了${it.data.size}个工程。", it)
                                view.initOk(mapper.translate(bingDingResponse))
                            }
                            else -> {
                                view.showError(it.msg)
                                view.initOk(null)
                            }
                        }
                    }
                }
                else -> view.initOk(null)
            }
        }
    }

    override fun onPresenterCreate() {
        super.onPresenterCreate()
        indexData()
    }


    override fun auth(username: String, password: String) {
        view.showLoading()
        when (username.isEmpty()) {
            true -> {
                view.showTipDialogError("用户名不能为空")
                return
            }
        }

        userInteractor.clearData(RequestType.XMB_AUTH)

        fetch(userInteractor.xmbAuth(username, password), RequestType.XMB_AUTH) {
            view.hideLoading()
            when (it.success) {
                true -> view.authOk(it, username)
                else -> view.showError(it.msg)
            }
        }
    }

    override fun onRequestError(errorCode: String?, errorMessage: String?) {
        view.hideLoading()
        view.showError(errorMessage)
    }
}